# -*- coding: utf-8 -*-

from rest_framework import serializers

from ops.cmdb.models import Desktop
from dvadmin.utils.serializers import CustomModelSerializer
from dvadmin.utils.viewset import CustomModelViewSet


class DesktopSerializer(CustomModelSerializer):
    """
    字典-序列化器
    """

    class Meta:
        model = Desktop
        fields = "__all__"
        read_only_fields = ["id"]


class ExportDesktopProfileSerializer(CustomModelSerializer):
    """
    资产导出 序列化器
    """
    # last_login = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    # dept__deptName = serializers.CharField(source='dept.deptName', default='')
    fee_type = serializers.CharField(source='get_fee_type_display', default='22')
    devices_ownner = serializers.CharField(source='devices_ownner.name', default='')
    devices_dept = serializers.CharField(source='devices_dept.name', read_only=True)

    class Meta:
        model = Desktop
        fields = ('devices_name', 'devices_num', 'devices_type', 'devices_usages', 'devices_dept', 'devices_ownner', 'fee_type', 'fee_number')


class DesktopViewSet(CustomModelViewSet):
    """
    字典管理接口
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = Desktop.objects.all()
    serializer_class = DesktopSerializer
    extra_filter_backends = []
    permission_classes = []
    search_fields = ["devices_name", "devices_num", "devices_type", "remark"]
    export_field_label = ['设备名称', '设备编码', '设备型号', '设备用于', '归属部门', '使用者', '费用类型', '费用']
    export_serializer_class = ExportDesktopProfileSerializer
